package cn.kgc.springbootdemo.util;

import cn.kgc.springbootdemo.constant.CommonConstants;
import cn.kgc.springbootdemo.enums.ResultEnum;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <h3>springbootdemo</h3>
 * <p>响应信息主体</p>
 *
 * @author : DongZeJian
 * @date : 2020-04-27 19:57
 **/
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    @ApiModelProperty(value = "返回标记：成功标记=0，失败标记=1")
    private Integer code;

    @Getter
    @Setter
    @ApiModelProperty(value = "返回信息")
    private String msg;

    @Getter
    @Setter
    @ApiModelProperty(value = "判断是否成功")
    private Boolean success;

    @Getter
    @Setter
    @ApiModelProperty(value = "数据")
    private T data;

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMessage();
    }

    public R(String msg, Boolean success, T data) {
        this.data = data;
        this.msg = msg;
        this.success = success;
        reuseCheck();
    }


    public R(String msg, Integer code, Boolean success, T data) {
        this.data = data;
        this.code = code;
        this.msg = msg;
        this.success = success;
        reuseCheck();
    }


    public static <T> R<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, ResultEnum.SUCCESS_CODE.getMessage());
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, ResultEnum.SUCCESS_CODE.getMessage());
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, CommonConstants.FAIL, ResultEnum.ERROR_CODE.getMessage());
    }

    public static <T> R<T> failed(String msg) {
        return failedResult(CommonConstants.FAIL, msg);
    }
    public static <T> R<T> failed(ResultEnum resultEnum) {
        return failedResult(resultEnum.getCode(), resultEnum.getMessage());
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, ResultEnum.ERROR_CODE.getMessage());
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    private static <T> R<T> failedResult(int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        return apiResult;
    }


    private void reuseCheck(){
        msg = StringUtils.isBlank(msg) ? "操作成功." : msg;
        success = success == null ? true : success;
        code = code == null ? success ? 200 : 0 : code;
    }

}

